package com.gzyz.cheetah.controller;


import cn.hutool.core.bean.BeanUtil;
import com.gzyz.cheetah.annotations.AdminJwtIntercept;
import com.gzyz.cheetah.annotations.UserJwtIntercept;
import com.gzyz.cheetah.entity.dto.ListIdDTO;
import com.gzyz.cheetah.entity.dto.UserDTO;
import com.gzyz.cheetah.entity.dto.PageDTO;
import com.gzyz.cheetah.entity.pagerequest.UserPageRequestRequest;
import com.gzyz.cheetah.entity.table.User;
import com.gzyz.cheetah.service.IUserService;
import com.gzyz.cheetah.util.Result;
import com.gzyz.cheetah.util.ThreadLocalUtil;
import com.gzyz.cheetah.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xclhove
 * @since 2023-11-06
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService iuserService;
    @GetMapping("/login")
    public Result <Object> login(@RequestParam String username,@RequestParam String password) {
        String token = iuserService.login(username, password);
        return Result.success(token);
    }

    @UserJwtIntercept
    @PostMapping("/logout")
    public Result <Object> logout() {
        Integer id = (Integer) ThreadLocalUtil.get("id");
        iuserService.logout(id);
        return Result.success();
    }


    @PostMapping("/register")
    public Result<Object> register(@RequestBody UserDTO userDTO) {
        User user = BeanUtil.copyProperties(userDTO, User.class);
        iuserService.register(user);
        return Result.success(user);
    }


    @AdminJwtIntercept
    @PostMapping("/resetpassword/{id}")
    public Result <Object> resetPassword(@PathVariable Integer id) {
        iuserService.resetpassword(id);
        return Result.success();
    }

    @UserJwtIntercept
    @GetMapping("/changePassword")
    public Result <Object> changePassword(HttpServletRequest request,@RequestParam String password) {
        String requestHeader = request.getHeader("Authorization");
        int id = TokenUtil.getId(requestHeader);
        iuserService.changepassword(id,password);
        return Result.success();
    }

    @UserJwtIntercept
    @PostMapping("/searchUser")
    public Result <User> searchUser() {
        Integer id = (Integer) ThreadLocalUtil.get("id");
        User user = iuserService.searchUser(id);
        return Result.success(user);
    }

    @AdminJwtIntercept
    @PostMapping("/delete/{id}")
    public Result <Object> delete(@PathVariable Integer id) {
        iuserService.delete(id);
        return Result.success();
    }

    @AdminJwtIntercept
    @PostMapping("/deleteByIds")
    public Result <Object> deleteByIds(@RequestBody ListIdDTO listIdsDTO) {
        iuserService.deleteByIds(listIdsDTO.getIds());
        return Result.success();
    }
    @UserJwtIntercept
    @PostMapping("/update")
    public Result <Object> update(@RequestBody User user) {
        iuserService.update(user);
        return Result.success();
    }

    @AdminJwtIntercept
    @PostMapping("/pageListAndSearch")
    public Result <PageDTO<User>> pageList(@RequestBody UserPageRequestRequest userPageRequest) {
        PageDTO<User> pagelist = iuserService.pageListAndSearch(userPageRequest);
        return Result.success(pagelist);
    }


    @AdminJwtIntercept
    @PostMapping("/freezeUserById/{id}")
    public Result <Object> freezeUserById(@PathVariable Integer id) {
        iuserService.freezeUserById(id);
        return Result.success();
    }

    @AdminJwtIntercept
    @PostMapping("/unFreezeUserById/{id}")
    public Result <Object> unFreezeUserById(@PathVariable Integer id) {
        iuserService.unFreezeUserById(id);
        return Result.success();
    }


    @AdminJwtIntercept
    @PostMapping("/freezeUsersByIds")
    public Result <Object> freezeUsersByIds(@RequestBody ListIdDTO listIdsDTO) {
        iuserService.freezeUsersByIds(listIdsDTO.getIds());
        return Result.success();
    }


    @AdminJwtIntercept
    @PostMapping("/unFreezeUsersByIds")
    public Result <Object> unFreezeUsersByIds(@RequestBody ListIdDTO listIdsDTO) {
        iuserService.unFreezeUsersByIds(listIdsDTO.getIds());
        return Result.success();
    }

    @GetMapping("/verifyAndChangePass")
    public Result <Object> verifyAndChangePass(@RequestParam String code , @RequestParam String password,@RequestParam String email){
        iuserService.verifyAndChangePass(code,password,email);
        return Result.success();
    }

    @GetMapping("/getCode")
    public Result <Object> creatCode(@RequestParam String username,@RequestParam String email){
        String code = iuserService.getCode(username,email);
        return Result.success(code);
    }
    
    @AdminJwtIntercept
    @PostMapping("/changeStorageSize")
    public Result <Object> changeStorageSize(@RequestBody User user) {
        iuserService.changeStorageSize(user);
        return Result.success();
    }
}
